package com.aatrox.bhsypro.util;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import jdk.nashorn.internal.runtime.regexp.joni.Config;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author dsn
 * @createTime 07 21:07
 * @description 七牛云工具
 */
public class QiniuUpload {

    // 设置需要操作的账号的AK和SK
    private static final String ACCESS_KEY = "6-5jb3Qly0IP3lHHu41Z3zLV4LXSmZWFqfVcjrpv";
    private static final String SECRET_KEY = "c3Fsalhu9tfrLpK95MMyx8-0i7YkqLB_W8vKrgsj";
    // 要上传的空间名
    private static final String bucketname = "imamarealyman";
    private static final String domain = "http://r1xjdohxw.hn-bkt.clouddn.com/";       //外链域名
    // 密钥
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    //    //上传
    public static String upload(File file) throws IOException {
        // 创建上传对象，Zone*代表地区
        Configuration configuration = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(configuration);
        try {
            // 调用put方法上传
            String token = auth.uploadToken(bucketname);
            if(StringUtils.isEmpty(token)) {
                System.out.println("未获取到token，请重试！");
                return null;
            }
            String imageName = Generate.shen();
            System.out.println("File name = "+imageName);
            Response res = uploadManager.put(file,imageName,token);
            // 打印返回的信息
            if (res.isOK()){
                return imageName;
            }
        }catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            e.printStackTrace();
            System.out.println("error "+r.toString());
            try {
                // 响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                System.out.println("error "+e1.error());
            }
        }
        return null;
    }

    public static void deleteOnQn(String key,String bucket) throws Exception{
        try {
            Auth auth = Auth.create(ACCESS_KEY,SECRET_KEY);
            Configuration configuration = new Configuration(Zone.zone2());
            BucketManager bucketManager = new BucketManager(auth,configuration);
            bucketManager.delete(key,bucket);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}